//
//  ShowComposeViewControlller.m
//  groupecards
//
//  Created by Alan Chen on 12/13/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ShowComposeViewControlller.h"

@implementation ShowComposeViewControlller

@synthesize delegate = delegate_;
@synthesize recipients = recipients_;
@synthesize subject = subject_;
@synthesize content = content_;

+ (BOOL)canSendEmail
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    // We must always check whether the current device is configured for sending emails
    return [mailClass canSendMail];
}
+ (BOOL)canSendSms
{
    Class smsClass = (NSClassFromString(@"MFMessageComposeViewController"));
    // We must always check whether the current device is configured for sending sms
    return [smsClass canSendText];
}


#pragma mark -
#pragma mark send sms or send email
//show the send sms view

- (void)displaySmsViewForController:(id)controller
{
	MFMessageComposeViewController *smsController = [[MFMessageComposeViewController alloc] init];
	smsController.messageComposeDelegate = self;
	[smsController setRecipients:self.recipients];
	[smsController setBody:self.content];
	[controller presentModalViewController:smsController animated:YES];
    [smsController release];
}
//show the send mail view
- (void)displayMailViewForController:(id)controller
{
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
	[picker setSubject:self.subject];
	// Set up recipients
	[picker setToRecipients:self.recipients];
	[picker setCcRecipients:nil];	
	[picker setBccRecipients:nil];
    
	NSString *emailBody = self.content;
	[picker setMessageBody:emailBody isHTML:NO];
	[controller presentModalViewController:picker animated:YES];
    [picker release];
}

#pragma mark MFMailComposeViewControllerDelegate methods
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{	
	[delegate_ composeController:self didFinishWithResultForMail:result error:error];
}

#pragma mark MFMessageComposeViewControllerDelegate methods
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
	[delegate_ composeController:self didFinishWithResultForMessage:result];
}

- (void)dealloc
{
    [content_ release];
    [subject_ release];
    [recipients_ release];
    delegate_ = nil;
    [super dealloc];
}
@end
